
import { agentEnterNewHouse, queryCompanyHouseResident } from "../../../service/platform_service"
import debounce from "../../../tools/debounce"
import { MAP_KEY } from "../../../service/base_service"

const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    themeStyle: "",
    showSharePop: false,
    isLogin: false,
    keyBoardHeight: 0,
    showEmptySharePop: false,//没有共享组的情况，创建共享组
    swiperAutoplay: false,
    swiperInterval: 2000,
    swiperDuration: 500,
    singleFormatShowText: "暂无",//单独的户型显示字段
    currentSwiperIndex: 0,//当前swiper下标
    isShowCommentPop: false,//评论弹窗
    //地图key -- 地图相关
    mapSubkey: MAP_KEY,
    latitude: 30.690885,
    longitude: 104.078367,
    enable3d: false,
    showCompass: false,
    enableOverlooking: false,
    enableZoom: false,
    enableScroll: false,
    enableRotate: false,
    drawPolygon: false,
    enableSatellite: false,
    enableTraffic: false,
    markers: [{
      id: 0,
      iconPath: "../../img/dingwei.png",
      latitude: 30.690885,
      longitude: 104.078367,
      width: 24,
      height: 24,
      callout: {

      }
    }],
    tempCommissionList: [
      { type: 1 }, { type: 1 }
    ],
    nearestSubWayText: "",//最近地铁信息
    showRulePopup: false,
    showPopupTitle: "推荐规则",
    showRuleContent: "",
    scanExpert: {},//是否有扫描的经纪人
    scrollRefreshStatus: true,
    changePriceTitle: "变价提醒",
    changeOpenTimeTitle: "售卖提醒",
    //吸顶参数
    scrollTop: 0,
    offsetTop: 0,
    toView: "",
    //吸顶参数
    //顶部信息骨架屏配置
    topRowWidths: [
      "60%", "100%"
    ],

    topBanners: [

    ],
    tempTags: [
      {
        title: "未开盘",
        status: 1,
      },
      {
        title: "低单价",
        status: 0,
      },
      {
        title: "低总价",
        status: 0,
      },
      {
        title: "多轨交汇",
        status: 0,
      },
      {
        title: "环线房",
        status: 0,
      },
    ],
    ratingValue: 4,//评论评星
    loadingStatus: false,//数据加载状态
    horizontalModules: [
      { title: "户型", isShow: true }, { title: "资料", isShow: true }, { title: "佣金", isShow: true }, { title: "配套", isShow: true },
      { title: "总平", isShow: true }, { title: "专家", isShow: true }, { title: "动态", isShow: true }, { title: "评论", isShow: true },
    ],
    horizontalSwiperIndex: 100,//默认户型
    houseTypeChangeArr: [

    ],//户型切换数组
    houseTypeList: [

    ],
    houseTypeChangeIndex: 0,//户型默认第一个

    attachFiles: [],//项目资料
    settingRoleRelation: [],//佣金列表
    commentList: [

    ],
    //动态列表
    houseDynamicList: [

    ],
    expertArr: [

    ],
    tempHouseTimeAxiss: [
      { type: 1, status: 0 }, { type: 1, status: 1 }, { type: 1, status: 0 }, { type: 1, status: 0 }
    ],

    houseNumber: null,//房源编号
    houseDetail: {},//新房详情
    formats: [],//户型数组
    //创建评论参数
    createCommentData: {
      content: "",
      images: [],
      houseNumber: "",
      parentId: "",
      grandId: "",
    },
    //评论的点击数据，评论成功后，直接强更，不去这个拉取列表
    commentData: {},
    commentType: "main",//main是代表一级评论，sub是代表二级带回复的评论
    //一级评论的index:
    oneLevelIndex: 0,
    currentSearchTitle: "公交",
    currentBottomIndex: 0,
    mapSearchList: [],
    changeTypeList: [
      {
        title: "公交",
        icon: "./../../img/mapgongjiao.png"
      },
      {
        title: "学校",
        icon: "./../../img/mapxuexiao.png"
      },
      {
        title: "餐饮",
        icon: "./../../img/mapcanyin.png"
      },
      {
        title: "购物",
        icon: "./../../img/mapgouwu.png"
      },
      {
        title: "医院",
        icon: "./../../img/mapyiyuan.png"
      }
    ],
    enterModles: [
      {
        title: "全部户型",
        path: "../../img/houseDetail/dquanbhx_icon.png",
        url: "../houseAllHouseTypes/houseAllHouseTypes",
      },
      {
        title: "楼盘动态",
        path: "../../img/houseDetail/dloupandt_icon.png",
        url: "../houseAllDyn/houseAllDyn",
      },
      {
        title: "楼盘详情",
        path: "../../img/houseDetail/dloupanxiangq_icon.png",
        url: "../houseMoreInfo/houseMoreInfo",
      },
      {
        title: "楼盘图片",
        path: "../../img/houseDetail/dloupantp_icon.png",
        url: "../houseAllImgs/houseAllImgs",
      },
      {
        title: "周边配套",
        path: "../../img/houseDetail/dzhoubpeitao_icon.png",
        url: "../houseMapAround/houseMapAround",
      },

    ],
    // 共享相关参数
    newShareName: "",//新建共享组名称
    shareList: [

    ],
    showExpertPop: false,//咨询置业顾问弹窗
    bindUserId: "",//绑定关系的userId,
    isScanEnter: false,//是否是扫码进来的
    isShare: false,//是否分享进来的
    roleType: 1,//角色等级
    zhuChangeArr: [],
    videoArr: [],//房源视频arr;
    allTopImgArr: [],//房源所有图片arr
    houseTypeIndex: 0,
    scrollTopValue: 0,//到顶部距离
    bindHouse: 0,//是否是绑定房源进来的
    selectCompanyNumber: "",//选中的代理公司
    residentArr: [],//驻场arr
    cityName: "",
    isShowMoreInfo: false,

    sameHouseList: [
      { type: 1 }, { type: 1 }, { type: 1 }, { type: 1 }
    ],
    communityNumber: "",//小区number
    communityDetail: null,//小区详情

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.communityNumber) {
      this.setData({
        communityNumber: options.communityNumber,

      })
    }







   



  },







  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if (app.globalData.themeStyle) {
      this.setData({
        themeStyle: app.globalData.themeStyle
      })
    }
    this.setData({
      isLogin: app.globalData.isLogin
    })
    this.setData({
      roleType: app.globalData.roleType || 1
    })
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    //显示顶部刷新图标
    // wx.showNavigationBarLoading();
    this.refreshPageMethod();
    console.log("JJLog:下拉刷新开始了===");
    //要刷新请求服务器的方法
    // this.selectCar();
    //隐藏导航栏加载框
    // wx.hideNavigationBarLoading();
    //停止下拉事件
    // wx.stopPullDownRefresh();
  },

  //下拉刷新页面方法
  refreshPageMethod() {
    wx.showLoading({
      title: '刷新中...',
    })
    setTimeout(() => {
      wx.hideLoading();
      wx.stopPullDownRefresh();
    }, 2000);
  },

  //顶部swiper切换方法:
  topImgSwiperChange(e) {
    this.setData({
      currentSwiperIndex: e.detail.current
    })
  },




  //户型切换点击:
  houseTypeChangeActions(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      houseTypeChangeIndex: index,
      houseTypeList: this.data.formats[index]
    })

  },

  //楼盘动态图片预览
  previewDynImg(e) {
    let index = e.currentTarget.dataset.index;
    let type = e.currentTarget.dataset.type;
    let cIndex = e.currentTarget.dataset.cindex;
    let arr = [];
    if (type == "dyn") {
      arr = this.data.houseDynamicList;
    }
    let pic = arr[cIndex].pics[index].url;
    let pics = arr[cIndex].pics;
    let urls = [];
    pics.forEach((item) => {
      urls.push(item.url);
    })

    wx.previewImage({
      current: pic,
      urls: urls,
    })
  },

















 
  // 事件触发，调用接口
  nearby_search: function () {
    let _this = this;
  
  },
  //周边地铁请求:
  nearby_searchSubWay: function () {
   
  },














  //跳转到房源位置
  pushAavigationAdd() {
    let latitude = this.data.communityDetail.latitude;
    let longitude = this.data.communityDetail.longitude;
    wx.openLocation({
      latitude,
      longitude,
      scale: 15,
      name: this.data.communityDetail.communityName,
      address: this.data.communityDetail.address
    })
  },








  //跳转到周边配套页面
  pushToMapAround() {
    return;
    let expertItem = this.data.expertArr[0];
    app.globalData.expertData = expertItem;
    //准备报备楼盘信息
    let reportData = {
      houseNumber: this.data.houseDetail.houseNumber,
      name: this.data.houseDetail.name,
      reportCardReq: this.data.houseDetail.reportCardReq,
      reportPhoneReq: this.data.houseDetail.reportPhoneReq,
    }
    app.globalData.reportHouseData = reportData;

    app.globalData.houseLat = this.data.houseDetail.latitude;
    app.globalData.houseLon = this.data.houseDetail.longitude;
    app.globalData.mapHouseAddress = this.data.houseDetail.houseAddress
    wx.navigateTo({
      url: '/newHousePackage/pages/houseMapAround/houseMapAround?name=' + this.data.houseDetail.name + "&houseNumber=" + this.data.houseDetail.houseNumber
    })
  },



  clickCenterPopupOverlay() {
    this.setData({
      showSharePop: false,
      showEmptySharePop: false,
      showRulePopup: false,
    })
  },














  //scrollView下拉刷新
  refreshDownData() {
    this.setData({
      scrollRefreshStatus: true
    })
    return;

  },

  //回到首页
  backHomeAction() {
    wx.switchTab({
      url: '/pages/home/home',
    })
  },
  onScroll(event) {
    wx.createSelectorQuery()
      .select('#scroller')
      .boundingClientRect((res) => {
        this.setData({
          scrollTop: event.detail.scrollTop,
          offsetTop: res.top,
        });
      })
      .exec();
  },
  //滚动点击:
  horizontalTapAction(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      horizontalSwiperIndex: index
    })
  },


  keyBoardHeightChange(e) {
    let height = e.detail.height;
    console.log("JJLog:高度变化时:" + height)
    this.setData({
      keyBoardHeight: height,
    })
  },


  //预览视频:
  previewImgOrVideo() {
    //视频
    let videoUrl = this.data.videoArr[0].url;
    app.globalData.videoUrl = videoUrl;
    wx.navigateTo({
      url: '/pages/videoPlay/videoPlay',
    })
  },
  //预览图片:
  previewImgAction(e) {
    let url = e.currentTarget.dataset.url;
    let arr = e.currentTarget.dataset.arr;
    let urls = [];
    arr.forEach((item) => {
      urls.push(item.url);
    })
    wx.previewImage({
      current: url,
      urls: urls,
    })
  },
  // 户型顶部切换:
  houseTypeChangeAction(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      houseTypeIndex: index
    })
    if (index == 0) {
      this.setData({
        topBanners: this.data.videoArr,
        currentSwiperIndex: 0
      })
    } else {
      this.setData({
        topBanners: this.data.allTopImgArr,
        currentSwiperIndex: 0
      })
      // console.log("JJLog:topBanners:" + JSON.stringify(this.data.topBanners));
    }
  },


  //滑动监听
  bindscrollAction(e) {
    // console.log("JJLog:滚动监听:" + JSON.stringify(e));
    let scrollTopValue = e.detail.scrollTop;
    this.setData({
      scrollTopValue: scrollTopValue,
    })
  },

  //查看佣金弹窗
  seeRuleDetailAction(e) {
    let text = e.currentTarget.dataset.text;
    let title = e.currentTarget.dataset.title;
    this.setData({
      showRuleContent: text,
      showPopupTitle: title,
      showRulePopup: true
    })
  },

  //顶部功能模块跳转:
  pushToEnterMAction(e) {
    let item = e.currentTarget.dataset.item;
    let url = item.url;
    let houseNumber = this.data.houseDetail.houseNumber;
    if (item.title == "周边配套") {
      this.pushToMapAround();
      return;
    }
    if (item.title == "全部户型") {
      if (this.data.houseTypeChangeArr && this.data.houseTypeChangeArr.length < 1) {
        wx.showToast({
          title: '该楼盘暂无户型信息',
          icon: "none"
        })
      } else {
        wx.navigateTo({
          url: url + '?houseNumber=' + houseNumber,
        })
      }
      return;
    }
    wx.navigateTo({
      url: url + '?houseNumber=' + houseNumber,
    })
  },


  //绑定房源操作:
  bindHouseAction() {
    if (this.data.roleType == 7) {
      //经纪人绑定:
      // wx.showModal({
      //   title: '提示',
      //   content: '确认入驻为该房源经纪人?',
      //   confirmColor: "#FF0000",
      //   success: res => {
      //     if (res.confirm) {
      //       this.agentEnterNewHouse();
      //     }
      //   }
      // })
      wx.navigateTo({
        url: '/formPackage/pages/platformModule/agentEnterHouseApply/agentEnterHouseApply?houseNumber=' + this.data.houseDetail.houseNumber,
      })
    } else if (this.data.roleType == 9) {
      //公司绑定:
      wx.navigateTo({
        url: '/formPackage/pages/commissionManager/addNewCommission/addNewCommission?houseNumber=' + this.data.houseDetail.houseNumber,
      })
    }
  },

  //经纪人入驻房源接口调用:
  agentEnterNewHouse() {
    wx.showLoading();
    agentEnterNewHouse(this.data.houseDetail.houseNumber).then(res => {
      wx.hideLoading();
      if (res.code == 200) {

      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "操作失败",
        icon: "none"
      })
      console.error("JJLog:agentEnterNewHouse", err);
    })
  },
  //入驻专家点击;
  expertEnterHouseAction() {
    if (!this.data.isLogin) {
      wx.navigateTo({
        url: '/pages/login/login',
      })
      return;
    }
    if (this.data.isLogin && this.data.roleType == 1) {
      //已经登录但是是普通人的时候:
      wx.navigateTo({
        url: '/formPackage/pages/enterApply/agentApply/agentApply',
      })
      return;
    }
    //已经是经纪人或者公司角色
    if (this.data.roleType == 7 || this.data.roleType == 9) {
      wx.navigateTo({
        url: '/formPackage/pages/platformModule/agentEnterHouseApply/agentEnterHouseApply?houseNumber=' + this.data.houseDetail.houseNumber,
      })
    }
  },

  //代理公司驻场点击:
  residentShowPopAction(e) {
    let item = e.currentTarget.dataset.item;
    // console.log("JJLog:item=" + JSON.stringify(item));
    this.setData({
      selectCompanyNumber: item.companyNumber
    })
    this.queryCompanyHouseResident();
  },
  //查询代理公司的驻场员工:
  queryCompanyHouseResident() {
    wx.showLoading();
    queryCompanyHouseResident(this.data.selectCompanyNumber, this.data.houseDetail.houseNumber).then(res => {
      wx.hideLoading();
      console.log("JJLog:查询到的驻场是:" + JSON.stringify(res));
      if (res.code == 200) {
        this.setData({
          residentArr: res.data || []
        })
        if (res.data && res.data.length > 0) {
          this.setData({
            showExpertPop: true,
            expertArr: this.data.residentArr
          })
        } else {
          wx.showToast({
            title: '暂无代理驻场',
            icon: "none"
          })
        }
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || "查询失败",
        icon: "none"
      })
      console.error("JJLog:queryCompanyHouseResident", err);
    })
  },



  //查看更多的展开收起:
  showMoreBtnClick() {
    this.setData({
      isShowMoreInfo: !this.data.isShowMoreInfo
    })
  },





  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let path = "";
    path = "/newHousePackage/pages/newHouseDetail/newHouseDetail?isShare=1" + "&userId=" + app.globalData.userId + "&houseNumber=" + this.data.houseDetail.houseNumber

    return {
      title: this.data.houseDetail.name || app.globalData.commonShareTitle,
      path: path
    }
  },
  onShareTimeline: function () {
    return {
      title: this.data.houseDetail.name || app.globalData.commonShareTitle,
      query: "",
      imageUrl: this.data.houseDetail.coverPic
    }
  },
})